﻿using System;
using System.Data;
using System.Text.RegularExpressions;

namespace TrinityAPI.Base.Tools
{
    public class TypeConverter
    {
        #region ToBool
        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(object expression, bool defValue)
        {
            if (expression != null)
                return StrToBool(expression, defValue);

            return defValue;
        }

        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(string expression, bool defValue)
        {
            if (expression != null)
            {
                if (string.Compare(expression, "true", true) == 0)
                    return true;
                else if (string.Compare(expression, "false", true) == 0)
                    return false;
            }
            return defValue;
        }
        #endregion

        #region ToInt
        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object expression)
        {
            return ObjectToInt(expression, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object expression, int defValue)
        {
            if (expression != null && expression != DBNull.Value)
                return StrToInt(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型,转换失败返回0
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str)
        {
            return StrToInt(str, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str, int defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;

            int rv;
            if (Int32.TryParse(str, out rv))
                return rv;

            return Convert.ToInt32(StrToFloat(str, defValue));
        }
        #endregion

        #region ToFloat
        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的float类型结果</returns>
        public static float StrToFloat(object strValue, float defValue)
        {
            if (strValue == null)
                return defValue;

            return StrToFloat(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的float类型结果</returns>
        public static float ObjectToFloat(object strValue, float defValue)
        {
            if (strValue == null)
                return defValue;

            return StrToFloat(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的float类型结果</returns>
        public static float ObjectToFloat(object strValue)
        {
            return ObjectToFloat(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <returns>转换后的float类型结果</returns>
        public static float StrToFloat(object strValue)
        {
            if (strValue == null)
                return 0;

            return StrToFloat(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的float类型结果</returns>
        public static float StrToFloat(string strValue, float defValue)
        {
            if ((strValue == null) || (strValue.Length > 10))
                return defValue;

            float intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                    float.TryParse(strValue, out intValue);
            }
            return intValue;
        }
        #endregion

        #region ToDecimal
        /// <summary>
        /// string型转换为decimal型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的decimal类型结果</returns>
        public static decimal StrToDecimal(object strValue, decimal defValue)
        {
            if (strValue == null)
                return defValue;

            return StrToDecimal(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为decimal型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static decimal ObjectToDecimal(object strValue, decimal defValue)
        {
            if (strValue == null)
                return defValue;

            return StrToDecimal(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为decimal型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static decimal ObjectToDecimal(object strValue)
        {
            return ObjectToDecimal(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为decimal型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static decimal StrToDecimal(object strValue)
        {
            if ((strValue == null))
                return 0;

            return StrToDecimal(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为decimal型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static decimal StrToDecimal(string strValue, decimal defValue)
        {
            if ((strValue == null) || (strValue.Length > 10))
                return defValue;

            decimal intValue = defValue;
            if (strValue != null)
            {
                bool IsDecimal = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsDecimal)
                    decimal.TryParse(strValue, out intValue);
            }
            return intValue;
        }
        #endregion

        #region 枚举与string int转换
        /// <summary>
        /// 枚举转换成整型数组
        /// </summary>
        /// <typeparam name="T"> 枚举类型</typeparam>
        /// <returns></returns>
        public static int[] EnumToInt<T>()
        {
            try
            {
                return (int[])Enum.GetValues(typeof(T));
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 把字符串或整型转换成数组
        /// </summary>
        /// <typeparam name="T"> 枚举类型</typeparam>
        /// <param name="value"> 字符串或整型值 </param>
        /// <param name="defalutvalue"> 默认值</param>
        /// <returns></returns>
        public static T EnumByValue<T>(object value, object defalutvalue)
        {
            try
            {
                return (T)Enum.Parse(typeof(T), value.ToString());
            }
            catch (Exception)
            {
                return (T)Enum.Parse(typeof(T), defalutvalue.ToString());
            }
        }

        /// <summary>
        /// 把整型转换成枚举字符串
        /// </summary>
        /// <typeparam name="T"> 枚举类型</typeparam>
        /// <param name="value"> 整型值</param>
        /// <returns></returns>
        public static string EnumIntToString<T>(int value)
        {
            try
            {
                return Enum.GetName(typeof(T), value);
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 枚举转换成字符串数组
        /// </summary>
        /// <typeparam name="T"> 枚举类型</typeparam>
        /// <returns></returns>
        public static string[] EnumToString<T>()
        {
            try
            {
                return Enum.GetNames(typeof(T));
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region DateTime To String
        public static string DateTimeFormat(DateTime datetime, string format)
        {
            if (string.IsNullOrEmpty(format))
                return string.Empty;
            try
            {
                return datetime.ToString(format);
            }
            catch
            {
                return string.Empty;
            }
        }
        #endregion
    }
}
